<?php
define('INI_FILE', dirname(__FILE__) . '/config.ini');
define('PATHROOT', dirname(__FILE__) . "/");
define('SERVERNAME', "http://tddp1.no-ip.biz/TDDP1-Server/");
define('SERVER_UPLOADS_NAME', "http://tddp1.no-ip.biz/TDDP1-ServerUploads/");

$ini = parse_ini_file(INI_FILE, true);

//validate data types:
$data = array("general" => array(
					"debug" => array("type" => 'bool', "define" => "DEBUG"),
					"tempFilesPath" => array("type" => 'string', "define" => "TEMP_PATH"),
					"uploadFilesPath" => array("type" => 'string', "define" => "UPLOADPATH"),
					"minPasswordAllowedLength" => array("type" => 'numeric', "define" => "MINPASSLENGTH"),
					"maxPasswordAllowedLength" => array("type" => 'numeric', "define" => "MAXPASSLENGTH"),
					"adminPassword" => array("type" => 'string', "define" => "USER_ROOT_PASS"),
					"languages" => array("type" => 'string', "define" => "LANGUAGES"),
					"logFile" => array("type" => 'string', "define" => "LOGFILE"),
					"logMaxSize" => array("type" => 'string', "define" => "LOGFILEMAXSIZE"),
					"useFixedNearUsers" => array("type" => 'bool', "define" => "USEFIXEDUSERS"),
				),
				"location" => array(
					"defaultLang" => array("type" => 'string', "define" => "DEFAULTLANG"),
					"timeZone" => array("type" => 'string', "define" => "TIMEZONE"),
				),
				"openfire" => array(
					"useOpenfire" => array("type" => 'string', "define" => "USEOPENFIRE"),
					"openfireRootURL" => array("type" => 'string', "define" => "OPENFIRE_ROOT"),
					"openfireUserServiceFolder" => array("type" => 'string', "define" => "OPENFIRE_USER_SERVICE"),
					"openfireSecretKey" => array("type" => 'string', "define" => "OPENFIRE_USER_SECRET")
				),
                "database" => array(
                    "name" => array("type" => 'string', "define" => "DB_NAME"),
                    "username" => array("type" => 'string', "define" => "DB_USER"),
                    "password" => array("type" => 'string', "define" => "DB_PASSWORD"),
                    "host" => array("type" => 'string', "define" => "DB_HOST"),
                )
			);

foreach ($data as $sectionKey => $section){
	foreach ($section as $key => $config){
		$validate = "is_" . $config["type"];
		if(!isset($ini[$sectionKey][$key])){
			if(!defined("PARSE_INI_ERROR")){
				define("PARSE_INI_ERROR", true);
				define("PARSE_INI_ERROR_CONFIG", $sectionKey . " " . $key . " not exists");
			}
		}
		$value = $ini[$sectionKey][$key];

		// this is done because parse init put everything as a string
		switch($config["type"]){
			case "bool":
				$value = (bool)$value;
				break;
		}

		if(!$validate($value)){
			if(!defined("PARSE_INI_ERROR")){
				define("PARSE_INI_ERROR", true);
				define("PARSE_INI_ERROR_CONFIG", $sectionKey . " " . $key);
				break;
			}
		}else{
			if($config["define"]){
				define($config["define"], $value);
			}
		}
	}
}

if(!defined("PARSE_INI_ERROR")){
	define("PARSE_INI_ERROR", false);
}
